#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <string>
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <fstream>
#include <sys/stat.h>
#include <fcntl.h>
#include <sstream>

/*
  Path for Satvik : /home/nsatvik/Projects/DC_Project/GUI\ v1.0/GUI/client
  Path for Samir: /home/samiriff/Project/GUI\ v1.0/GUI/client
 */


using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    path = "/home/nsatvik/Projects/DC_Project/DCP-v8.0/Client/";
    setupCompanyImage();
    createServerFileList();
    displayFileList();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setupCompanyImage()
{
    QImage imageObject;
    imageObject.load("/home/nsatvik/Projects/DC_Project/GUI\ v1.0/GUI/bg3.jpg");
    ui->label_4->setPixmap(QPixmap::fromImage(imageObject));
}

void MainWindow::buttonClientHandler()
{
    ui->comboBox->addItem("abc");
    //ui->label->setText(ui->lineEdit->text());

    string filenameStr = getFilenameFromLineEdit();
    transferFileFromServer(filenameStr);
    displayFileList();
}

string MainWindow::getFilenameFromLineEdit()
{
    QString filenameQStr = ui->lineEdit->text();
    string filenameStr = filenameQStr.toStdString();
    return filenameStr;
}

void MainWindow::createServerFileList()
{
    cout<<"HAHAHAHAHA SAMIR"<<endl;
    chdir(path.c_str());
    system("pwd");
    string command = "./client 127.0.0.1 -list";
    cout<<command<<endl;
    system(command.c_str());
//    pid_t childpid = vfork();
//    if(childpid == 0)
//    {
//        ui->label_3->setText("Loading File List");

////        QString filenameQStr = ui->lineEdit_2->text();
////        string filenameStr = filenameQStr.toStdString();

////        QString clientPathQStr = ui->lineEdit_2->text();
////        string clientPathStr = clientPathQStr.toStdString();
////        execl(clientPathStr.c_str(), "client", "127.0.0.1", filenameStr.c_str(), (char*)0);

//        //execl("/home/nsatvik/Projects/DC_Project/GUI\ v1.0/GUI/client", "client", "127.0.0.1", "-list", (char*)0);
//    }
//    else
//        if(childpid == -1)
//            ui->label->setText("Child Not Created");


    ui->label_3->setText("Completed Loading File List");
}

void MainWindow::displayFileList()
{
    initFileList();
}

void MainWindow::initFileList()
{
    QList<QString>  fileList;

    ifstream infile;
    infile.open((path+"file.list").c_str());

    string filename;
    ui->comboBox->clear();
    while(getline(infile, filename))
    {
        fileList.append(QString::fromStdString(filename));
    }

    infile.close();

    QStringList filestrlist(fileList);

    ui->comboBox->insertItems(0, filestrlist);
}

void MainWindow::transferFileFromServer(string filenameStr)
{
    chdir(path.c_str());
    string command = "./client 127.0.0.1 "+filenameStr;
    system(command.c_str());
//    pid_t dummyChildpid = vfork();

//    if(dummyChildpid == 0)
//    {
//        pid_t childpid = vfork();
//        if(childpid == 0)
//        {
//            ui->label_3->setText("File Of Interest --> " + ui->lineEdit->text());
//            //execl("/home/nsatvik/Projects/DC_Project/GUI\ v1.0/GUI/client", "client", "127.0.0.1", filenameStr.c_str(), (char*)0);

//            QString clientPathQStr = ui->lineEdit_2->text();
//            string clientPathStr = clientPathQStr.toStdString();
//            execl(clientPathStr.c_str(), "client", "127.0.0.1", filenameStr.c_str(), (char*)0);
//        }

//        //int time_sec = 0;
//        /*while(waitpid(childpid, NULL, 0) != childpid)
//        {
//            //sleep(1);
//            string timeStr = "Time Elapsed = " + time_sec;
//            ui->label_3->setText(QString::fromStdString(timeStr));
//        }*/

//        SleeperThread::msleep(2000);

        displayContentsOfFile(filenameStr);
//    }
}

void MainWindow::displayContentsOfFile(string filenameStr)
{
    ifstream infile;
    infile.open(filenameStr.c_str());

    string fileContentsStr = "";

    QString fileContentsQStr;
    char ch;
    while(infile.get(ch))
    {
        fileContentsStr += ch;
    }

    fileContentsQStr.append(QString::fromStdString(fileContentsStr));

    ui->textBrowser->setText(fileContentsQStr);
    infile.close();

    displayFileSizes(filenameStr);
}

void MainWindow::displayFileSizes(string filenameStr)
{
    struct stat st;

    //For the actual file
    lstat(filenameStr.c_str(), &st);
    int actualSize = st.st_size;

    string fileSizeStr = "Actual File Size = " + integerToString(actualSize) + " bytes";
    QString fileSizeQStr = QString::fromStdString(fileSizeStr);
    ui->label_9->setText(fileSizeQStr);


    //For the Compressed File
    filenameStr += ".rir";
    lstat(filenameStr.c_str(), &st);
    int compressedSize = st.st_size;

    fileSizeStr = "Compressed File Size = " + integerToString(compressedSize) + " bytes";
    fileSizeQStr = QString::fromStdString(fileSizeStr);
    ui->label_10->setText(fileSizeQStr);

    //Compression Ratio
    fileSizeStr = "Compression Ratio = " + integerToString(compressedSize *100 / actualSize) + " %";
    fileSizeQStr = QString::fromStdString(fileSizeStr);
    ui->label_11->setText(fileSizeQStr);
}


string MainWindow::integerToString(int n)
{
    stringstream intstr;

    intstr << n;
    string str = "" + intstr.str();

    return str;
}


void MainWindow::dropDownListButtonHandler()
{
    QString filenameQStr = ui->comboBox->currentText();
    string filenameStr = filenameQStr.toStdString();

    transferFileFromServer(filenameStr);
}


void MainWindow::dropDownListHandler()
{
    QString filenameQStr = ui->comboBox->currentText();
    string filenameStr = filenameQStr.toStdString();

    ui->label_3->setText("File Of Interest --> " + filenameQStr);
}


void MainWindow::textEditHandler()
{
    QString filenameQStr = ui->lineEdit->text();
    string filenameStr = filenameQStr.toStdString();

    ui->label_3->setText("File Of Interest --> " + filenameQStr);
}



void MainWindow::ButtonRefreshHandler()
{
    initFileList();
}
